﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace 流量控制试验台
{
    public class DAM
    {
        public int RetryTimes = 3;
        public byte TargetAddr = 1;
        public int RetryInterval = 500;
        public SerialPort Com;
        public DAM(SerialPort _com)
        {
            Com = _com;
        }
        public float Proc7401()
        {
            int retry = RetryTimes;
	        while(retry!=0)
	        {
                Com.DiscardInBuffer();
                Com.Write("#020\r");
                Com.ReadTimeout = 1000;
                string rx;
                while (true)
                {
                    try
                    {
                        rx = Com.ReadTo("\r");
                    }
                    catch
                    {
                        break;
                    }
                    if(rx[0]=='>')
                    {
                        if(rx.Length==8)
                        {
                            try
                            {
                                return float.Parse(rx.Substring(1));
                            }
                            catch
                            {
                                break;
                            }
                        }
                    }
                }
                Thread.Sleep(RetryInterval);
		        retry--;
	        }
            return float.NaN;
        }
        public bool ProcDA1(float da)
        {
            int retry = RetryTimes;
            while (retry != 0)
            {
                Com.DiscardInBuffer();
                if(da>0)
                    Com.Write("#01D0+"+da.ToString("00.000")+"\r");
                else
                    Com.Write("#01D0" + da.ToString("00.000") + "\r");
                Com.ReadTimeout = 1000;
                string rx;
                while (true)
                {
                    try
                    {
                        rx = Com.ReadTo("\r");
                    }
                    catch
                    {
                        break;
                    }
                    if (rx == "!01")
                    {
                        return true;
                    }
                }
                Thread.Sleep(RetryInterval);
                retry--;
            }
            return false;
        }
        public bool ProcDA2(float da)
        {
            int retry = RetryTimes;
            while (retry != 0)
            {
                Com.DiscardInBuffer();
                if (da > 0)
                    Com.Write("#01D0+" + da.ToString("00.000") + "\r");
                else
                    Com.Write("#01D0" + da.ToString("00.000") + "\r");
                Com.ReadTimeout = 1000;
                string rx;
                while (true)
                {
                    try
                    {
                        rx = Com.ReadTo("\r");
                    }
                    catch
                    {
                        break;
                    }
                    if (rx == "!01")
                    {
                        return true;
                    }
                }
                Thread.Sleep(RetryInterval);
                retry--;
            }
            return false;
        } 
    }
}
